package controller;

import java.util.List;

import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;

import org.activiti.engine.HistoryService;
import org.activiti.engine.history.HistoricProcessInstance;
import org.activiti.engine.task.Task;
import org.springframework.dao.support.DaoSupport;

import com.web.util.WebUtils;

import wfm.activiti.ActivitiManager;
import wfm.entities.Ticket;
import wfm.entities.User;
import wfm.entities.WFInfo;
import wfm.query.QueryDao;

@ManagedBean(name="surveyorProcessController")
@RequestScoped
public class SurveyorProcessController {
	
	@ManagedProperty(value="#{fault}")
	private int fault=2; 
	
	@ManagedProperty(value="#{estimatedCosts}")
	private String estimatedCosts; 
	
	@ManagedProperty(value="#{description}")
	private String description; 
	
	@ManagedProperty(value = "#{user}")
	private User user;
	
	@ManagedProperty(value = "#{wfInfo}")
	private WFInfo wfInfo;
	
	private Ticket ticket; 
	
	
	public SurveyorProcessController(){
		
	}
	
	@PostConstruct
	public void init(){
		this.ticket = (Ticket) QueryDao.getVariableToProcessById(
				wfInfo.getInstanceId(), "ticket");
		String predFault = (String)QueryDao.getVariableToProcessById(
				wfInfo.getInstanceId(), "predFault");
		this.ticket.setPredictionFault(predFault);
	}
	
	public Ticket getTicket() {
		return ticket;
	}



	public void setTicket(Ticket ticket) {
		this.ticket = ticket;
	}



	public String finishSurveyorEstimation(){
		String nextPage = "myTasks";

		System.out.println("final Service");
		
		Ticket ticket = (Ticket) QueryDao.getVariableToProcessById(wfInfo.getInstanceId(), "ticket");
		ticket.setEstimatedCosts(Double.parseDouble(this.estimatedCosts));
		ticket.setSurveyorDescription(this.description);
		ticket.setFault(fault);
		QueryDao.saveVariableToProcessById(wfInfo.getInstanceId(), "ticket",ticket);
		

		// Retrieving Activiti engine
		ActivitiManager manager = ActivitiManager.getInstance();
		
		Task t = QueryDao.getTaskById(this.getTaskId());
		
		if(fault==2) { //Customer Fault
			QueryDao.saveVariableToProcessWithTask(t, "fault", "CustomerFault");
		}
		else{
			QueryDao.saveVariableToProcessWithTask(t, "fault", "OwnFault");
		}

		try {
			manager.getTaskRuntimeService().complete(this.getTaskId());

		} catch (Exception e) {
			e.printStackTrace();
			return "";
		}

		// Check for any execution errors
		String instanceId = this.getInstanceId();
		
		Task task=QueryDao.getNextTaskAndSetAssignee(instanceId, this.user);  //holt sich den nächsten Task zur Gruppe und setzt Asignee
		
		if(task!=null){
			this.setTaskId(task.getId());
			nextPage = QueryDao.getFormKey(this.getTaskId());
		}

		return WebUtils.redirect(nextPage);

	}
	
	/**
	 * Returns the UserGroup of the logged-in User
	 * @return
	 */
	private String getActUserGroup(){
		
		String userGroup=User.GROUP_DESKTOPWORKER;
		
		if(this.user!=null){
			userGroup = this.user.getUserGroup();
		}
		
		return userGroup;
	}
	
	public String abort(){
		return WebUtils.redirect("myTasks");
	}

	public int getFault() {
		return fault;
	}

	public void setFault(int fault) {
		this.fault = fault;
	}

	public String getEstimatedCosts() {
		return estimatedCosts;
	}

	public void setEstimatedCosts(String estimatedCosts) {
		this.estimatedCosts = estimatedCosts;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}
	
	public WFInfo getWfInfo() {
		return wfInfo;
	}

	public void setWfInfo(WFInfo wfInfo) {
		this.wfInfo = wfInfo;
	}

	public String getInstanceId() {
		return this.wfInfo.getInstanceId();
	}

	public void setInstanceId(String instanceId) {
		this.wfInfo.setInstanceId(instanceId);
	}

	public String getTaskId() {
		return this.wfInfo.getTaskId();
	}

	public void setTaskId(String taskId) {
		this.wfInfo.setTaskId(taskId);
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}
	
	
}
